package com.YHVision.qiuzhike.manager;

import com.YHVision.qiuzhike.common.ErrorCode;
import com.YHVision.qiuzhike.exception.BusinessException;
import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.*;
import io.reactivex.Flowable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 *  通用的AI调用工具(SDK封装)
 *
 * @author <a href="">YHVision-小y</a>
 * @from <a href="todo">爱编程学习圈</a>
 */
@Component
public class AIManager {

    @Resource
    private ClientV4 clientV4;

    //生成的答案较稳定
    private static final float STABLE_TEMPERATURE = 0.05f;

    //生成的答案较随机
    private static final float UNSTABLE_TEMPERATURE = 0.99f;

    /**
     * 通用同步调用(答案较随机)
     * @param systemMessage
     * @param userMessage
     * @return
     */
    public String doSyncUnStableRequest(String systemMessage,String userMessage) {
        return doRequest(systemMessage,userMessage,Boolean.FALSE,UNSTABLE_TEMPERATURE);
    }

    /**
     * 通用同步调用(答案稳定)
     * @param systemMessage
     * @param userMessage
     * @return
     */
    public String doSyncStableRequest(String systemMessage,String userMessage) {
        return doRequest(systemMessage,userMessage,Boolean.FALSE, STABLE_TEMPERATURE);
    }

    /**
     * 通用同步调用(再简化)
     * @param systemMessage
     * @param userMessage
     * @param temperature
     * @return
     */
    public String doSyncRequest(String systemMessage,String userMessage, float temperature) {
        return doRequest(systemMessage,userMessage,Boolean.FALSE, temperature);
    }

    /**
     * 通用同步调用(简化)
     * @param systemMessage
     * @param userMessage
     * @param temperature
     * @return
     */
    public String doRequest(String systemMessage,String userMessage,Boolean stream, Float temperature) {
        List<ChatMessage> chatMassgeList = new ArrayList<>();
        ChatMessage systemChatMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), systemMessage);
        chatMassgeList.add(systemChatMessage);
        ChatMessage userChatMessage = new ChatMessage(ChatMessageRole.USER.value(), userMessage);
        chatMassgeList.add(userChatMessage);
        return doRequest(chatMassgeList,stream,temperature);
    }

    /**
     * 通用同步调用
     * @param messages
     * @param stream
     * @param temperature
     * @return
     */
    public String doRequest(List<ChatMessage> messages,Boolean stream, Float temperature) {
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model(Constants.ModelChatGLM4)
                .stream(stream)
                .temperature(temperature)
                .invokeMethod(Constants.invokeMethod)
                .messages(messages)
                .build();
        try {
            ModelApiResponse invokeModelApiResp = clientV4.invokeModelApi(chatCompletionRequest);
            return invokeModelApiResp.getData().getChoices().get(0).toString();
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,e.getMessage());
        }

    }

    /**
     * 通用流式调用(简化)
     * @param systemMessage
     * @param userMessage
     * @param temperature
     * @return
     */
    public Flowable<ModelData> doStreamRequest(String systemMessage,String userMessage, Float temperature) {
        List<ChatMessage> chatMassgeList = new ArrayList<>();
        ChatMessage systemChatMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), systemMessage);
        chatMassgeList.add(systemChatMessage);
        ChatMessage userChatMessage = new ChatMessage(ChatMessageRole.USER.value(), userMessage);
        chatMassgeList.add(userChatMessage);
        return doStreamRequest(chatMassgeList,temperature);
    }

    /**
     * 通用流式调用
     * @param messages
     * @param temperature
     * @return
     */
    public Flowable<ModelData> doStreamRequest(List<ChatMessage> messages, Float temperature) {
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model(Constants.ModelChatGLM4)
                .stream(Boolean.TRUE)
                .temperature(temperature)
                .invokeMethod(Constants.invokeMethod)
                .messages(messages)
                .build();
        try {
            ModelApiResponse invokeModelApiResp = clientV4.invokeModelApi(chatCompletionRequest);
            return invokeModelApiResp.getFlowable();
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,e.getMessage());
        }

    }



}
